import pygame
from typing import List, Dict, Tuple
import math

class Animation:
    def __init__(self, duration: float, delay: float = 0):
        self.duration = duration
        self.delay = delay
        self.elapsed = 0
        self.started = False
        self.finished = False
    
    def update(self, dt: float) -> bool:
        """更新动画状态，返回是否完成"""
        if self.finished:
            return True
            
        if not self.started:
            self.delay -= dt
            if self.delay <= 0:
                self.started = True
            return False
            
        self.elapsed += dt
        if self.elapsed >= self.duration:
            self.finished = True
            return True
        return False
    
    def get_progress(self) -> float:
        """获取动画进度 (0-1)"""
        if not self.started:
            return 0
        if self.finished:
            return 1
        return self.elapsed / self.duration

class FloatingText(Animation):
    def __init__(self, text: str, start_pos: Tuple[int, int], color: Tuple[int, int, int],
                 duration: float = 1.0, rise_height: int = 50):
        super().__init__(duration)
        self.text = text
        self.start_pos = start_pos
        self.color = color
        self.rise_height = rise_height
    
    def get_current_pos(self) -> Tuple[int, int]:
        progress = self.get_progress()
        x = self.start_pos[0]
        y = self.start_pos[1] - (self.rise_height * progress)
        alpha = int(255 * (1 - progress))
        return (x, y, alpha)

class CardAnimation(Animation):
    def __init__(self, start_pos: Tuple[int, int], end_pos: Tuple[int, int], 
                 duration: float = 0.3):
        super().__init__(duration)
        self.start_pos = start_pos
        self.end_pos = end_pos
    
    def get_current_pos(self) -> Tuple[int, int]:
        progress = self.get_progress()
        x = self.start_pos[0] + (self.end_pos[0] - self.start_pos[0]) * progress
        y = self.start_pos[1] + (self.end_pos[1] - self.start_pos[1]) * progress
        return (int(x), int(y))

class FlashAnimation(Animation):
    def __init__(self, target_rect: pygame.Rect, color: Tuple[int, int, int], 
                 duration: float = 0.2):
        super().__init__(duration)
        self.target_rect = target_rect
        self.color = color
    
    def get_alpha(self) -> int:
        progress = self.get_progress()
        return int(255 * (1 - progress))

class AnimationManager:
    def __init__(self):
        self.animations: List[Animation] = []
        self.floating_texts: List[FloatingText] = []
        self.card_animations: List[CardAnimation] = []
        self.flash_animations: List[FlashAnimation] = []
    
    def add_floating_text(self, text: str, pos: Tuple[int, int], color: Tuple[int, int, int]):
        """添加浮动文字动画"""
        self.floating_texts.append(FloatingText(text, pos, color))
    
    def add_card_animation(self, start_pos: Tuple[int, int], end_pos: Tuple[int, int]):
        """添加卡牌移动动画"""
        self.card_animations.append(CardAnimation(start_pos, end_pos))
    
    def add_flash_animation(self, target_rect: pygame.Rect, color: Tuple[int, int, int]):
        """添加闪烁动画"""
        self.flash_animations.append(FlashAnimation(target_rect, color))
    
    def update(self, dt: float):
        """更新所有动画"""
        # 更新浮动文字
        self.floating_texts = [anim for anim in self.floating_texts 
                             if not anim.update(dt)]
        
        # 更新卡牌动画
        self.card_animations = [anim for anim in self.card_animations 
                              if not anim.update(dt)]
        
        # 更新闪烁动画
        self.flash_animations = [anim for anim in self.flash_animations 
                               if not anim.update(dt)]
    
    def draw(self, screen: pygame.Surface, font: pygame.font.Font):
        """绘制所有动画"""
        # 绘制闪烁动画
        for anim in self.flash_animations:
            alpha = anim.get_alpha()
            flash_surface = pygame.Surface(anim.target_rect.size, pygame.SRCALPHA)
            flash_surface.fill((*anim.color, alpha))
            screen.blit(flash_surface, anim.target_rect)
        
        # 绘制浮动文字
        for text_anim in self.floating_texts:
            x, y, alpha = text_anim.get_current_pos()
            text_surface = font.render(text_anim.text, True, text_anim.color)
            text_surface.set_alpha(alpha)
            screen.blit(text_surface, (x - text_surface.get_width()//2, y)) 